home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C14 / Ccright.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.3 KB  |  61 lines

  1. //: C14:Ccright.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Correctly synthesizing the CC
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. class Parent {
  11.   int i;
  12. public:
  13.   Parent(int ii) : i(ii) {
  14.     cout << "Parent(int ii)\n";
  15.   }
  16.   Parent(const Parent& b) : i(b.i) {
  17.     cout << "Parent(Parent&)\n";
  18.   }
  19.   Parent() :i(0) { cout << "Parent()\n"; }
  20.   friend ostream&
  21.     operator<<(ostream& os, const Parent& b) {
  22.     return os << "Parent: " << b.i << endl;
  23.   }
  24. };
  25.  
  26. class Member {
  27.   int i;
  28. public:
  29.   Member(int ii) : i(ii) {
  30.     cout << "Member(int ii)\n";
  31.   }
  32.   Member(const Member& m) : i(m.i) {
  33.     cout << "Member(Member&)\n";
  34.   }
  35.   friend ostream&
  36.     operator<<(ostream& os, const Member& m) {
  37.     return os << "Member: " << m.i << endl;
  38.   }
  39. };
  40.  
  41. class Child : public Parent {
  42.   int i;
  43.   Member m;
  44. public:
  45.   Child(int ii) : Parent(ii), i(ii), m(ii) {
  46.     cout << "Child(int ii)\n";
  47.   }
  48.   friend ostream&
  49.     operator<<(ostream& os, const Child& d){
  50.     return os << (Parent&)d << d.m
  51.               << "Child: " << d.i << endl;
  52.   }
  53. };
  54.  
  55. int main() {
  56.   Child d(2);
  57.   cout << "calling copy-constructor: " << endl;
  58.   Child d2 = d; // Calls copy-constructor
  59.   cout << "values in d2:\n" << d2;
  60. } ///:~
  61.